// pages/myself/myspellgroup/MySpellGroup.js
var Zan = require('../../../dist/index.js');
let network = null;
let utils = null;
let api = null;
const app = getApp();
var start01 = 0;
var start02 = 0;

function countdown(that) {
  for (var i = 0; i < that.data.groupsdes.length;i++){
    var EndTime = that.data.groupsdes[i].endDate || [];
    var NowTime = new Date().getTime();
    var total_micro_second = EndTime - NowTime || [];
    // 渲染倒计时时钟
    that.data.groupsdes[i].clock = dateformat(total_micro_second)
    // that.setData({
    //   clock: dateformat(total_micro_second)
    // });
    if (total_micro_second <= 0) {
      that.data.groupsdes[i].clock = "时间截止"
      //return;
    }
  }
  that.setData({
    groupsdes: that.data.groupsdes
  })

  setTimeout(function () {
    total_micro_second -= 1000;
    countdown(that);
  }
    , 1000)
}

// 时间格式化输出，如11:03 25:19 每1s都会调用一次
function dateformat(micro_second) {
  // 总秒数
  var second = Math.floor(micro_second / 1000);
  // 天数
  var day = Math.floor(second / 3600 / 24);
  // 小时
  var hr = Math.floor(second / 3600 % 24);
  // 分钟
  var min = Math.floor(second / 60 % 60);
  // 秒
  var sec = Math.floor(second % 60);
  return day + "天" + hr + "小时" + min + "分" + sec + "秒";
}
Page(Object.assign({}, Zan.Tab, Zan.TopTips, Zan.Toast,{

  /**
   * 页面的初始数据
   */
  
  data: {
    tab: {
      list: [{
        id: 0,
        title: '正在开团'
      }, {
        id: 1,
        title: '正在参团'
      }],
      selectedId: 0,
      height: 40,
      scroll: false
    },
    groupsdes: [],
    activeIndex: 0,
    recordsTotal01: 0,
    recordsTotal02: 0,
    nodata: {
      msg: '没有拼团',
      notype: 1,
      gomsg: '去拼团',
      imgurl: '../../../images/nodatas.png',
      nourl: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    this.data.tab.selectedId = options.index;
    this.setData({
      tab: this.data.tab,
    })
    this.setData({
      activeIndex: options.index
    })

    this.onPullDownRefresh();
    //调用上面定义的递归函数，一秒一刷新时间
    // countdown(that);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    //status  1表示开团 0表示参团
    if (this.data.tab.selectedId == 0){
      start01 = 0;
      this.setData({
        groupsdes:[]
      })
      network._post_page_json(this, api.mySpellGroups, start01, { status: 1 }, success00);
    }else{
      start02 = 0;
      this.setData({
        groupsdes: []
      })
      network._post_page_json(this, api.mySpellGroups, start02, { status: 0 }, success02);
    }
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  
  // },
  handleZanTabChange(e){
    var componentId = e.componentId;
    var selectedId = e.selectedId;

    this.setData({
      [`${componentId}.selectedId`]: selectedId
    })

    this.setData({
      activeIndex:selectedId
    })

    if (this.data.tab.selectedId == 0) {
      start01 = 0;
      this.setData({
        groupsdes: []
      })
      network._post_page_json(this, api.mySpellGroups, start01, { status: 1 }, success00);
    } else {
      start02 = 0;
      this.setData({
        groupsdes: []
      })
      network._post_page_json(this, api.mySpellGroups, start02, { status: 0 }, success02);
    }

  },
  ongroupItemClick:function(e){
    var Cantuan_spec = {};
    var Cantuan_specs = [];
    var order = e.currentTarget.dataset.item.orderProductDetail;
    if (order.productSpec && !order.productSpec1 && !order.productSpec2){
      var Spec = e.currentTarget.dataset.item.spellGroups.specType1 + ':' + order.productSpec
    } else if (order.productSpec1 && !order.productSpec2){
      var Spec = e.currentTarget.dataset.item.spellGroups.specType1 + ':' + order.productSpec + ',' + e.currentTarget.dataset.item.spellGroups.specType2 + ':' + order.productSpec1
    } else if (order.productSpec2){
      var Spec = e.currentTarget.dataset.item.spellGroups.specType1 + ':' + order.productSpec + ',' + e.currentTarget.dataset.item.spellGroups.specType2 + ':' + order.productSpec1 + ',' + e.currentTarget.dataset.item.spellGroups.specType3 + ':' + order.productSpec2
    }
    var count = e.currentTarget.dataset.item.orderProductDetail.count;
    Cantuan_spec.Spec = Spec;
    Cantuan_spec.count = count
    Cantuan_specs.push(Cantuan_spec);
    try {
      wx.setStorageSync('Cantuan_spec', Cantuan_specs)
    } catch (e) {
    }
    var userSpellGroupId = e.currentTarget.dataset.id;
    var originator = e.currentTarget.dataset.item.originator;
    var spellGroupsId = e.currentTarget.dataset.item.spellGroupsId;
    var sgCombId = e.currentTarget.dataset.item.sgCombId ? e.currentTarget.dataset.item.sgCombId:'';
    var tuantype = e.currentTarget.dataset.tuantype;
    var endtime = e.currentTarget.dataset.item.endDate
    var clocktime = e.currentTarget.dataset.item.clock;
    if (clocktime =='时间截止'){
      this.showZanTopTips(this.prompt, '拼团失败,支付金额将原路返还！');
    }else{
      wx.navigateTo({
        url: '/pages/myself/myspellgroup/MySpellGroupDetails?userSpellGroupId=' + userSpellGroupId + '&spellGroupsId=' + spellGroupsId + '&sgCombId=' + sgCombId + '&type=' + tuantype + '&endtime=' + endtime + '&originator=' + originator
      })
    }
    

  },

  //跳转到团购列表页
  gotoHome:function(e){
    wx.switchTab({
      url: '/pages/product/SpellGroupList',
    })
  }

}))

var success00 = function(that,res){
  if(res.data.data){
    for(var i=0;i<res.data.data.length;i++){
      if (res.data.data[i].spellGroups) {
        if (res.data.data[i].spellGroups.specType1 && !res.data.data[i].spellGroups.specType2 && !res.data.data[i].spellGroups.specType3) {
          res.data.data[i].spellGroupsComb.specDetails = res.data.data[i].spellGroups.specType1 + ':' + res.data.data[i].orderProductDetail.productSpec
        }
        if (res.data.data[i].spellGroups.specType2 && !res.data.data[i].spellGroups.specType3) {
          res.data.data[i].spellGroupsComb.specDetails = res.data.data[i].spellGroups.specType1 + ':' + res.data.data[i].orderProductDetail.productSpec + '   ' + res.data.data[i].spellGroups.specType2 + ':' + res.data.data[i].orderProductDetail.productSpec1
        }
        if (res.data.data[i].spellGroups.specType3) {
          res.data.data[i].spellGroupsComb.specDetails = res.data.data[i].spellGroups.specType1 + ':' + res.data.data[i].orderProductDetail.productSpec + '   ' + res.data.data[i].spellGroups.specType2 + ':' + res.data.data[i].orderProductDetail.productSpec1 + '   ' + res.data.data[i].spellGroups.specType3 + ':' + res.data.data[i].orderProductDetail.productSpec2
        }
      }
      
    }
    that.setData({
      groupsdes: that.data.groupsdes.concat(res.data.data)
    })
  }
  countdown(that);
}

var success02 = function(that,res){
    if (res.data.data) {
      for (var i = 0; i < res.data.data.length; i++) {
        if (res.data.data[i].spellGroups) {
          if (res.data.data[i].spellGroups.specType1 && !res.data.data[i].spellGroups.specType2 && !res.data.data[i].spellGroups.specType3) {
            res.data.data[i].spellGroupsComb.specDetails = res.data.data[i].spellGroups.specType1 + ':' + res.data.data[i].orderProductDetail.productSpec
          }
          if (res.data.data[i].spellGroups.specType2 && !res.data.data[i].spellGroups.specType3) {
            res.data.data[i].spellGroupsComb.specDetails = res.data.data[i].spellGroups.specType1 + ':' + res.data.data[i].orderProductDetail.productSpec + '   ' + res.data.data[i].spellGroups.specType2 + ':' + res.data.data[i].orderProductDetail.productSpec1
          }
          if (res.data.data[i].spellGroups.specType3) {
            res.data.data[i].spellGroupsComb.specDetails = res.data.data[i].spellGroups.specType1 + ':' + res.data.data[i].orderProductDetail.productSpec + '   ' + res.data.data[i].spellGroups.specType2 + ':' + res.data.data[i].orderProductDetail.productSpec1 + '   ' + res.data.data[i].spellGroups.specType3 + ':' + res.data.data[i].orderProductDetail.productSpec2
          }
        }

      }
      that.setData({
        groupsdes: that.data.groupsdes.concat(res.data.data)
      })
    }
    countdown(that);
}



